﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CsharpCodeGenerator
{
    public class BllGenerator
    {
        #region SelectAllRows
        public static string SelectAllRows(string MethodName, string TableName, List<Sql> Columns, bool Static, List<ForeignKey> sqlListForeignKeys, bool IsGenEntity)
        {
            string IsStatic = (Static ? "static " : "");
            StringBuilder Code = new StringBuilder();
            Code.AppendLine(string.Format(CsharpHelper.BllClass, TableName));

            foreach (var item in Columns)
            {
                if (item.TableName == TableName)
                {
                    Code.AppendFormat(CsharpHelper.BllProperies, CsharpHelper.ConvertSqlToCsharp(item.DataType), item.ColumnName, Environment.NewLine);
                }
            }
            if (IsGenEntity)
            {
                var fItems = Columns.Where(m => m.IsForeignKey == "1" && m.TableName == TableName);
                if (fItems != null)
                {
                    foreach (var itemFK in fItems)
                    {
                        var PK_Table = sqlListForeignKeys.Where(m => m.FK_Column == itemFK.ColumnName && m.K_Table == TableName).SingleOrDefault();
                        Code.AppendFormat(CsharpHelper.BllProperiesEntity, PK_Table.PK_Table, PK_Table.PK_Table, Environment.NewLine,
                            string.Format("return {0}.{0}SelectRow({1});",
                            PK_Table.PK_Table, PK_Table.FK_Column));
                    }
                }

                var fItemsList = sqlListForeignKeys.Where(m => m.PK_Table == TableName).ToArray();
                if (fItemsList != null)
                {
                    foreach (var item1 in fItemsList)
                    {
                        Code.AppendFormat(CsharpHelper.BllProperiesEntity,
                            string.Format("List<{0}>", item1.K_Table),
                            item1.K_Table + "s", Environment.NewLine,
                            string.Format("return {0}.{0}SelectRowByForeignKey_{1}({2});", item1.K_Table, item1.FK_Column, item1.PK_Column)
                            );
                    }
                }
            }

            StringBuilder Params = new StringBuilder();

            foreach (var item in Columns)
            {
                if (item.TableName == TableName)
                {
                    Params.AppendLine(string.Format("\t\t\t\t\t{0} = !dr.IsDBNull(dr.GetOrdinal(\"{0}\")) ? ({1})dr[\"{0}\"] : {2},", item.ColumnName, CsharpHelper.ConvertSqlToCsharp(item.DataType), CsharpHelper.CsharpDefault(item.DataType)));
                }
            }
            string Paramaters = Params.ToString();
            Paramaters = Paramaters.Remove(Paramaters.Length - 3, 3); // Remove ',' from last of string

            Code.AppendLine(string.Format(CsharpHelper.BllSelectAll, IsStatic, TableName, TableName + "SelectAll", Paramaters));

            return Code.ToString();
        }
        #endregion

        #region SelectRow
        public static string SelectRow(string MethodName, string TableName, List<Sql> Columns, bool Static)
        {
            string IsStatic = (Static ? "static " : "");
            StringBuilder Code = new StringBuilder();
            string Paramerters = string.Empty;
            string ParamertersInput = string.Empty;
            StringBuilder Params = new StringBuilder();

            foreach (var item in Columns)
            {
                if (item.TableName == TableName)
                {
                    if (int.Parse(item.IsIdentity) != 0 || int.Parse(item.IsIndex) != 0)
                    {
                        Paramerters += string.Format("{0} {1}, ", CsharpHelper.ConvertSqlToCsharp(item.DataType), item.ColumnName);
                        ParamertersInput += string.Format("{0}, ", item.ColumnName);
                    }
                    Params.AppendLine(string.Format("\t\t\t\t\t{0} = !dr.IsDBNull(dr.GetOrdinal(\"{0}\")) ? ({1})dr[\"{0}\"] : {2},", item.ColumnName, CsharpHelper.ConvertSqlToCsharp(item.DataType), CsharpHelper.CsharpDefault(item.DataType)));
                }
            }

            Paramerters = Paramerters.Length > 1 ? Paramerters.Remove(Paramerters.Length - 2, 2) : ""; // Remove ',' from last of string
            ParamertersInput = ParamertersInput.Length > 1 ? ParamertersInput.Remove(ParamertersInput.Length - 2, 2) : ""; // Remove ',' from last of string
            string newParams = Params.ToString();
            newParams = newParams.Length > 2 ? newParams.Remove(newParams.Length - 3, 3) : "";

            Code.AppendLine(string.Format(CsharpHelper.BllSelectRow, IsStatic, TableName, TableName + MethodName, Paramerters, TableName.ToLowerInvariant(), Params, ParamertersInput));

            return Code.ToString();
        }
        #endregion

        #region SelectRowByForeignKey
        public static string SelectRowByForeignKey(string MethodName, string TableName, List<Sql> Columns, bool Static)
        {
            var fItems = Columns.Where(m => m.IsForeignKey == "1" && m.TableName == TableName);
            if (fItems != null)
            {
                StringBuilder Code = new StringBuilder();
                string IsStatic = (Static ? "static " : "");
                foreach (var itemFK in fItems)
                {

                    StringBuilder Params = new StringBuilder();

                    foreach (var item in Columns)
                    {
                        if (item.TableName == TableName)
                        {
                            Params.AppendLine(string.Format("\t\t\t\t\t{0} = !dr.IsDBNull(dr.GetOrdinal(\"{0}\")) ? ({1})dr[\"{0}\"] : {2},", item.ColumnName, CsharpHelper.ConvertSqlToCsharp(item.DataType), CsharpHelper.CsharpDefault(item.DataType)));
                        }
                    }

                    string Paramaters = Params.ToString();
                    Paramaters = Paramaters.Remove(Paramaters.Length - 3, 3); // Remove ',' from last of string

                    Code.AppendLine(string.Format(CsharpHelper.BllSelectByForeignKey, IsStatic, TableName, TableName + "SelectRowByForeignKey_" +
                        itemFK.ColumnName, Paramaters, itemFK.ColumnName, CsharpHelper.ConvertSqlToCsharp(itemFK.DataType)));
                }

                StringBuilder ParamsTemp = new StringBuilder();
                string ColumnName = "";
                string ParamsTempAll = "";
                string ParamsTempAllType = "";
                int itemp = 0;
                foreach (var itemFK in fItems)
                {
                    if (itemp == 0)
                    {
                        foreach (var item in Columns)
                        {
                            if (item.TableName == TableName)
                            {
                                ParamsTemp.AppendLine(string.Format("\t\t\t\t\t{0} = !dr.IsDBNull(dr.GetOrdinal(\"{0}\")) ? ({1})dr[\"{0}\"] : {2},",
                                    item.ColumnName, CsharpHelper.ConvertSqlToCsharp(item.DataType), CsharpHelper.CsharpDefault(item.DataType)));
                            }
                        }
                    }
                    ColumnName = ColumnName + "_" + itemFK.ColumnName;
                    ParamsTempAll += itemFK.ColumnName + ",";
                    ParamsTempAllType += CsharpHelper.ConvertSqlToCsharp(itemFK.DataType) + " " + itemFK.ColumnName + ",";
                    itemp++;
                }
                string ParamatersTemp = ParamsTemp.ToString();
                if (ParamatersTemp.Length > 3)
                {
                    ParamatersTemp = ParamatersTemp.Remove(ParamatersTemp.Length - 3, 3); // Remove ',' from last of string
                    ParamsTempAll = ParamsTempAll.Remove(ParamsTempAll.Length - 3, 3); // Remove ',' from last of string
                    ParamsTempAllType = ParamsTempAllType.Remove(ParamsTempAllType.Length - 3, 3); // Remove ',' from last of string
                    if (ColumnName == "" || Code.ToString().IndexOf(TableName + "SelectRowByForeignKey" + ColumnName) > -1)
                        return Code.ToString();
                    Code.AppendLine(string.Format(CsharpHelper.BllSelectByForeignKeyAll, IsStatic, TableName, TableName + "SelectRowByForeignKey" + ColumnName,
                        ParamatersTemp, ParamsTempAll, ParamsTempAllType));
                }
                
                return Code.ToString();
            }
            return "";
        }
        #endregion

        #region InsertRow
        public static string InsertRow(string MethodName, string TableName, List<Sql> Columns, bool Static)
        {
            string IsStatic = (Static ? "static " : "");
            StringBuilder Code = new StringBuilder();

            string Parameters = string.Empty;
            string Params = string.Empty;

            bool firstParam = true;

            foreach (var item in Columns)
            {
                if (item.TableName == TableName)
                {
                    if (int.Parse(item.IsIdentity) == 0)
                    {
                        if (firstParam)
                        {
                            firstParam = false;
                        }
                        Parameters += CsharpHelper.ConvertSqlToCsharp(item.DataType) + " " + item.ColumnName + ", ";
                        Params += item.ColumnName + ", ";
                    }
                }
            }

            Parameters = Parameters.Remove(Parameters.Length - 2, 2); // Remove ',' from last of string
            Params = Params.Remove(Params.Length - 2, 2);

            Code.AppendLine(string.Format(CsharpHelper.BllInsertRow, IsStatic, TableName + MethodName, Parameters, Params, TableName));

            return Code.ToString();
        }
        #endregion

        #region UpdateRow
        public static string UpdateRow(string MethodName, string TableName, List<Sql> Columns, bool Static)
        {
            string IsStatic = (Static ? "static " : "");
            StringBuilder Code = new StringBuilder();

            string Parameters = string.Empty;
            string Params = string.Empty;

            foreach (var item in Columns)
            {
                if (item.TableName == TableName)
                {
                    //if (int.Parse(item.IsIdentity) != 0 || int.Parse(item.IsIndex) != 0)
                    {
                        Parameters += CsharpHelper.ConvertSqlToCsharp(item.DataType) + " " + item.ColumnName + ", ";
                    }
                    Params += item.ColumnName + ", ";
                }
            }
            Parameters = Parameters.Length > 1 ? Parameters.Remove(Parameters.Length - 2, 2) : ""; // Remove ',' from last of string
            Params = Params.Length > 1 ? Params.Remove(Params.Length - 2, 2) : "";

            Code.AppendLine(string.Format(CsharpHelper.BllUpdateRow, IsStatic, TableName + MethodName, Parameters, Params, TableName));

            return Code.ToString();
        }
        #endregion

        #region DeleteRow
        public static string DeleteRow(string MethodName, string TableName, List<Sql> Columns, bool Static)
        {
            string IsStatic = (Static ? "static " : "");
            StringBuilder Code = new StringBuilder();

            string Parameters = string.Empty;
            string Params = string.Empty;

            foreach (var item in Columns)
            {
                if (item.TableName == TableName)
                {
                    if (int.Parse(item.IsIdentity) != 0 || int.Parse(item.IsIndex) != 0)
                    {
                        Parameters += CsharpHelper.ConvertSqlToCsharp(item.DataType) + " " + item.ColumnName + ", ";
                        Params += item.ColumnName + ", ";
                    }
                }
            }
            Parameters = Parameters.Length > 1 ? Parameters.Remove(Parameters.Length - 2, 2) : ""; // Remove ',' from last of string
            Params = Params.Length > 1 ? Params.Remove(Params.Length - 2, 2) : "";

            Code.AppendLine(string.Format(CsharpHelper.BllDeleteRow, IsStatic, TableName + MethodName, Parameters, Params, TableName));

            return Code.ToString();
        }
        #endregion
    }
}
